package com.xintoucloud.gsv.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.SuperVision;
import com.xintoucloud.gsv.entity.SuperVisionState;
import com.xintoucloud.gsv.entity.SupervisionType;
import com.xintoucloud.gsv.model.DeptData;
import com.xintoucloud.gsv.service.DeptService;
import com.xintoucloud.gsv.service.SuperVisionService;
import com.xintoucloud.gsv.service.SupervisionTypeService;

@Controller@RequestMapping("/superVision")
public class SuperVisionController {
	@Autowired
	private SupervisionTypeService supervisionTypeService;
	 
	
	@Autowired
	private SuperVisionService superVisionService;
	
	@Autowired
	private DeptService deptTypeService;
	
	@RequestMapping("/toSuperVision")
	public String toSuperVision(Model m) {
		List<Dept> depts=deptTypeService.listDeptWithParentIdIsFirst();
		m.addAttribute("depts", depts);
		List<SupervisionType> superVisionTypes=supervisionTypeService.list();
		m.addAttribute("superVisionTypes", superVisionTypes);
		return "superVision";
		
	}
	@RequestMapping("/toGetDepts")
	@ResponseBody
	public DeptData toGetDepts(int parentId) {
		DeptData depts=deptTypeService.listDeptByParentId1(parentId);
		return depts;
		
	}
	@PostMapping("/toAddSuperVision")
	public String toAddSuperVision(SuperVision superVision,int sid,HttpServletRequest request) {
		String[] dids=request.getParameterValues("did");
		int realdid=0;
		for (String did : dids) {
			System.out.println("did:"+did);
			if(did.equals("")==false) {
			realdid=Integer.parseInt(did);
			}
		}
		SupervisionType supervisionType=new SupervisionType();
		supervisionType.setSid(sid);
		superVision.setSupervisionType(supervisionType);
		Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        superVision.setStartTime(sdf.format(date));
		Dept dept=new Dept();
		dept.setDid(realdid);
		superVision.setDept(dept);
		//发起督办时修改其状态为“已发起状态”
		SuperVisionState superVisionState=new SuperVisionState();
		//System.out.println("设置状态为2");
		superVisionState.setStid(2);
		superVision.setSuperVisionState(superVisionState);
		superVisionService.save(superVision);
		return "redirect:toSuperVision";
		
	}
	@GetMapping("/toAddSuperVision")
	public String toAddSuperVision1(SuperVision superVision,int sid,HttpServletRequest request) {
		String[] dids=request.getParameterValues("did");
		int realdid=0;
		for (String did : dids) {
			//System.out.println("did:"+did);
			realdid=Integer.parseInt(did);
		}
		SupervisionType supervisionType=new SupervisionType();
		supervisionType.setSid(sid);
		superVision.setSupervisionType(supervisionType);
		Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        superVision.setStartTime(sdf.format(date));
		Dept dept=new Dept();
		dept.setDid(realdid);
		superVision.setDept(dept);
		//发起督办时修改其状态为“已发起状态”
		SuperVisionState superVisionState=new SuperVisionState();
		//System.out.println("设置状态为1");
		superVisionState.setStid(1);
		superVision.setSuperVisionState(superVisionState);
		superVisionService.save(superVision);
		return "redirect:toSuperVision";
		
	}
	@PostMapping("/updateSuperVision")
	public String updateSuperVision(SuperVision superVision,int sid,HttpServletRequest request) {
		String[] dids=request.getParameterValues("did");
		int realdid=0;
		for (String did : dids) {
			//System.out.println("did:"+did);
			realdid=Integer.parseInt(did);
		}
		SupervisionType supervisionType=new SupervisionType();
		supervisionType.setSid(sid);
		superVision.setSupervisionType(supervisionType);
		Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        superVision.setStartTime(sdf.format(date));
		Dept dept=new Dept();
		dept.setDid(realdid);
		superVision.setDept(dept);
		//发起督办时修改其状态为“已发起状态”
		SuperVisionState superVisionState=new SuperVisionState();
		//System.out.println("设置状态为2");
		superVisionState.setStid(1);
		superVision.setSuperVisionState(superVisionState);
		superVisionService.update(superVision);
		return "redirect:../toSuperVisionDraft";
		
	}
}
